﻿using IDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class Base<T>:IBase<T> where T : class
    {
        public Base(MyContext context)
        {
            Context = context;
        }

        public MyContext Context { get; }

        public async Task<int> AddAsync(T t)
        {
            Context.Set<T>().Add(t);
            return await Context.SaveChangesAsync();
        }

        public async Task<int> DeleteAsync(int id)
        {
            var m=Context.Set<T>().Find(id);
            Context.Set<T>().Remove(m);
            return await Context.SaveChangesAsync();
        }

        public T GetValue(int id)
        {
            return Context.Set<T>().Find(id);
        }

        public IQueryable<T> Show()
        {
            return Context.Set<T>();
        }

        public IQueryable<T> ShowWhere(Expression<Func<T, bool>> predicate)
        {
            return Context.Set<T>().Where(predicate);
        }

        public async Task<int> UpdateAsync(T t)
        {
            Context.Set<T>().Update(t);
            return await Context.SaveChangesAsync();
        }
    }
}
